/**
 * 费用基础设置
 */
import React, { Component } from 'react';
import { connect } from 'react-redux'
import { Popconfirm, Popover, Badge, Radio, message } from 'antd'
import { messages } from "share/common"
import errorMessage from 'share/errorMessage'
import SlideFrameDetail from './slide-frame-detail'
import TableList from '../table-list'
import settingService from '../request-and-expense-settings.service'
import menuRoute from "routes/menuRoute";
import {batchOperations} from "components/template/wrapped-table-params/batch-operations";

const RadioGroup = Radio.Group;

class CostBaseSetting extends Component {

  state = {
    loading: false,
  };

  infoData = {
    dataSource: { explain: messages('expense-5.key1')/*您可在此页面配置【替票】【转交】功能是否开启，设置费用金额相关校验规则*/ },
    attributes: [{ title: messages('expense-5.key2')/*说明*/, dataIndex: 'explain', span: 24 }]
  };

  slideFrameInfo = {
    title: messages('expense-5.key3')/*费用基础设置*/,
    content: SlideFrameDetail
  };

  extraMessage = messages('expense-5.key4')/*配置费用详情是否显示替票控件。为“是”则费用详情页显示替票控件；为“否”则不显示。报销单默认打印员工填写的替票理由，可至<a onclick=\"window.open('/main/setting/expense-setting', '_blank')\">【报销设置】</a>页面更改设置*/;

  defaultInfo = {
    title: messages('expense-5.key3')/*费用基础设置*/,
    data: [{
      title: messages('expense-5.key5')/*功能设置*/,
      titleStyle: { marginTop: 0 },
      label: messages('expense-5.key6')/*是否允许【替票】*/,
      value: messages('common.no')/*否*/,
      extra: <span dangerouslySetInnerHTML={{__html: this.extraMessage}} />
    }, {
      label: messages('expense-5.key7')/*是否允许费用【转交】*/,
      value: messages('common.no')/*否*/,
      extra: messages('expense-5.key8')/*配置费用是否允许转交给他人。为“是”则允许且账本内显示转交按钮；为“否”则不允许且账本内不显示转交按钮*/
    }, {
      label: messages('expense-5.key9')/*是否允许批量导入费用*/,
      value: messages('common.no')/*否*/,
      extra: messages('expense-5.key10')/*配置是否允许批量导入费用。为“是”则在报销单详情页显示【批量导入费用】功能按钮；为“否”则不显示功能按钮*/
    }, {
      label: messages('expense-5.key11')/*是否配置费用分摊相关人范围*/,
      value: messages('common.no')/*否*/,
      extra: messages('expense-5.key12')/*配置为“否”则费用分摊相关人为账套下所有人，配置为“是”则费用分摊相关人范围为报销单参与人加费用归属人*/
    }, {
      label: messages('expense-5.key13')/*是否校验费用与发票一致性*/,
      value: messages('common.yes')/*是*/,
      extra: messages('expense-5.key14')/*配置费用关联单张发票时，是否校验费用与关联的的发票信息是否一致。为“否”，则不校验；为“是”，则根据勾选范围，仅字段有值时校验，存在任意字段不一致则在费用上标记“与发票不一致”*/
    }, {
      title: messages('expense-5.key15')/*金额校验*/,
      label: messages('expense-5.key16')/*费用总金额是否可为0*/,
      value: messages('common.no')/*否*/,
      extra: messages('expense-5.key17')/*配置费用金额是否可为0*/
    }, {
      label: messages('expense-5.key18')/*费用金额与商务卡金额校验*/,
      value: messages('expense-5.key19')/*小于等于*/,
      extra: messages('expense-5.key20')/*按照配置比较费用金额与费用上关联的商务卡消费记录总金额*/
    }, {
      label: messages('expense-5.key21')/*费用金额与发票价税合计总和校验*/,
      value: messages('expense-5.key19')/*小于等于*/,
      extra: messages('expense-5.key22')/*按照配置比较费用金额与费用上关联的发票价税合计总和（如果允许重复发票生成费用，则对于重复发票会跨费用校验）*/
    }, {
      label: messages('expense-5.key23')/*里程补贴费用金额是否等于补贴金额*/,
      value: messages('expense-5.key19')/*小于等于*/,
      extra: messages('expense-5.key24')/*按照配置比较里程补贴费用金额与补贴金额*/
    }]
  };

  searchForm = [{
    id: 'levelCode',
    dataIndex: 'levelCode',
    label: messages('expense-5.key25')/*层级*/,
    component: (
      <RadioGroup>
        <Radio value="COMPANY">{messages('expense-5.key26')/*公司*/}</Radio>
        <Radio value="TENANT">{messages('expense-5.key27')/*租户*/}</Radio>
      </RadioGroup>
    ),
    tagParser: value => {
      return value === "COMPANY" ? messages('expense-5.key26')/*公司*/ : messages('expense-5.key27')/*租户*/;
    }
  }, {
    id: 'distLevelOrgIds',
    dataIndex: 'distLevelOrgName',
    label: messages('expense-5.key28')/*启用公司*/,
    component: 'Chooser',
    props: {
      labelKey: 'name',
      valueKey: 'id',
      type: 'priority_company'
    },
    parser: value => ({
      distLevelOrgIds: value ? value.map(item => item.id) : null
    })
  }];

  columns = [{
    title: messages('expense-5.key25')/*层级*/,
    dataIndex: 'levelCode',
    width: '10%',
    render: value => value === 'COMPANY' ? messages('expense-5.key26')/*公司*/ : messages('expense-5.key27')/*租户*/
  }, {
    title: messages('expense-5.key29')/*所属组织*/,
    dataIndex: 'levelOrgName',
    render: value => <Popover content={value} placement="topLeft">{value}</Popover>
  }, {
    title: messages('expense-5.key28')/*启用公司*/,
    dataIndex: 'distLevelOrgName',
    render: value => <Popover content={value} placement="topLeft">{value}</Popover>
  }, {
    title: messages('common.column.status')/*状态*/,
    dataIndex: 'enabled',
    width: '10%',
    render: value => (
      <Badge
        status={value ? 'success' : 'error'}
        text={value ? messages('common.status.enable')/*启用*/ : messages('common.status.disable')/*禁用*/}
      />
    )
  }, {
    title: messages('common.operation')/*操作*/,
    dataIndex: 'invoiceOptId',
    width: '10%',
    render: (value, record) => (
      record.readOnly ? (
        <span onClick={e => e.stopPropagation()} className="font-disabled">{messages('common.delete')/*删除*/}</span>
      ) : (
        <Popconfirm
          title={messages('common.confirm.delete')/*确定要删除吗？*/}
          onConfirm={() => this.handleDelete(value)}
        >
          <a onClick={e => e.stopPropagation()}>{messages('common.delete')/*删除*/}</a>
        </Popconfirm>
      )
    )
  }];

  service = (page, size, searchParams) => {
    return settingService.getCostBaseSettingList(page, size, searchParams);
  };

  handleDelete = id => {
    this.setState({ loading: true });
    settingService.deleteCostBaseSetting([id]).then(() => {
      this.setState({ loading: false });
      this.advancedTableRef.getList();
      message.success(messages('common.delete.success',{arg1: ''})/*{arg1} 删除成功*/);
    }).catch(e => {
      this.setState({ loading: false });
      errorMessage(e.response);
    })
  };

  render() {
    const { loading } = this.state;
    let tableParams =  batchOperations({
      rowKey: 'invoiceOptId'
    }, [{
      type: 'DELETE',
      service: settingService.batchDeleteCostBaseSetting,
      onOk: () => this.advancedTableRef.getList()
    }, {
      type: 'ENABLE',
      service: settingService.batchOpenOrCloseCostBaseSetting,
      onOk: () => this.advancedTableRef.getList()
    }, {
      type: 'DISABLE',
      service: settingService.batchOpenOrCloseCostBaseSetting,
      onOk: () => this.advancedTableRef.getList()
    }], this);
    return (
      <div className="full-height-page">
        <TableList
          infoData={this.infoData}
          slideFrameInfo={this.slideFrameInfo}
          defaultInfo={this.defaultInfo}
          afterClose={refresh => typeof refresh === 'boolean' && refresh && this.advancedTableRef.getList()}
          tableParams={{
            loading,
            rowKey: 'invoiceOptId',
            searchForm: this.searchForm,
            columns: this.columns,
            service: this.service,
            onRef: ref => this.advancedTableRef = ref,
            ...tableParams
          }}
        />
      </div>
    )
  }
}

function mapStateToProps(state) {
  return { }
}

export default connect(mapStateToProps)(CostBaseSetting)
